#include "glare.gles2.Texture.h"
#include "glare.gles2.GpuResourceFactory.h"

namespace Glare {

	GLES2Texture::GLES2Texture(GLES2GpuResourceFactory* factory, const TextureDescription& description)
		: Texture(description)
		, _factory(factory)
	{
		glGenTextures(1, &_gl_texture_id);
		EGL_CHECK_ERROR;
	}

	GLES2Texture::~GLES2Texture()
	{
		glDeleteTextures(1, &_gl_texture_id);
		EGL_CHECK_ERROR;
	}

	GpuResource::Interface* GLES2Texture::_GetInterface()
	{
		return this;
	}

	GpuResourceFactory* GLES2Texture::GetFactory() const
	{
		return _factory;
	}

	const GLuint& GLES2Texture::GetGLES2Id() const
	{
		return _gl_texture_id;
	}

	void GLES2Texture::_UpdateImpl(const uint& dest_sub_resource, Box* dest_box, const void* src_data, const uint& src_row_pitch, const uint& src_depth_pitch)
	{
		glBindTexture(GL_TEXTURE_2D, _gl_texture_id);
		EGL_CHECK_ERROR;

		const TextureDescription& tex_desc = GetDescription();

		// TODO: use glTexSubImage2D()
		glTexImage2D(
			GL_TEXTURE_2D, 
			0, 
			GL_RGBA, /* this should be determined by the texture format*/
			tex_desc.Width, 
			tex_desc.Height, 
			0, 
			GL_RGBA, 
			GL_UNSIGNED_BYTE, 
			src_data);
		EGL_CHECK_ERROR;
	}
}
